<?php

namespace core\entity;

/**
 * Sắp xếp
 *
 * @author BapCai
 */
class Sort {

    private $orderBy = '';
    private $start = true;

    const DESC = 'DESC';
    const ASC = 'ASC';

    public function __construct($key = null, $direction = 'ASC') {
        if ($key != null && $key != '') {
            $this->orderBy = '`' . $key . '` ' . ($direction == Sort::DESC ? Sort::DESC : Sort::ASC);
            $this->start = false;
        }
    }

    public function andSort($key, $direction = 'ASC') {
        if (!$this->start) {
            $this->orderBy .= ' ,';
        }
        if ($key != null && $key != '') {
            $this->orderBy .= '`' . $key . '` ' . ($direction == Sort::DESC ? Sort::DESC : Sort::ASC);
            $this->start = false;
        }
        return $this;
    }

    public function __toString() {
        return $this->orderBy;
    }

}
